from widgets.scriptWidgets.ScriptWidget import ScriptWidget
from PySide6.QtWidgets import QLabel, QComboBox
from enums.WidgetType import WidgetType
from config import ScriptList


class CallScript(ScriptWidget):
    def __init__(self, initType=None):
        super().__init__(WidgetType.CALL_SCRIPT)
        self.initType = initType

        self.labTitle = QLabel(self)
        self.labTitle.setText('调用脚本:')
        self.labTitle.setFixedSize(55, self.lineHeight)
        self.addWidget(self.labTitle)

        self.newLineSpaceWidth = self.labTitle.width() + self.intervalX

        # script
        self.comScript = QComboBox(self)
        self.comScript.addItems(ScriptList.scriptNames())
        self.comScript.setCurrentIndex(0)
        self.addWidget(self.comScript)

        if initType == 'edit' or initType == 'create':
            self.addOperate()

    @property
    def data(self):
        return {
            "type": self.type.value,
            'script_id': ScriptList.scriptList[self.comScript.currentIndex()]['id'],
            'enable': self.switchEnable.isChecked()
        }

    @data.setter
    def data(self, data):
        self.setData(data)
        script = ScriptList.getById(data['script_id'])
        if script is not None:
            self.comScript.setCurrentText(script['name'])
